package twi;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import jtools.files.TextFile;
import jtools.files.filesystem.FileOperation;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.vocabulary.RDFS;

public class KeySearch {

	public static boolean isValid(String text, ArrayList<String> domainList) {
		throw new UnsupportedOperationException("Not yet implemented");
	}

	public static void main(String[] arg) throws IOException {
		System.out.println(RDFS.Class.getLocalName());
		System.out.println(RDFS.comment.getNameSpace());
		File f = new File(
				"D:\\workspace\\Jena\\src\\com\\hp\\hpl\\jena\\vocabulary");
		ArrayList<File> list = FileOperation.readAllFilesOfDirectory(f
				.getAbsolutePath(), -1);
		TextFile tf = new TextFile();
		tf.add("package twi_project.core;");
		tf.add("import com.hp.hpl.jena.vocabulary.*;");
		tf.add("public class Vocabulary {");
		ArrayList<String> prefLIst = new ArrayList<String>();
		ArrayList<String> arrays = new ArrayList<String>();

		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).isDirectory())
				continue;
			String fn = list.get(i).getName().replace(".java", "").replace(
					list.get(i).getParent(), "");
			TextFile in = new TextFile();
			in.readFile(list.get(i), false);
			String pref = null;
			String varList = "public static final String[] " + fn + "_list ={";
			while (in.hasNext()) {
				String line = in.getNextLine();
				String res = null;
				if (line.contains("public static final Property ")) {
					line = line.replace("public static final Property ", "");
					line = line.trim();
					res = line.split(" ")[0];
				} else if (line.contains("public static final Resource ")) {
					line = line.replace("public static final Resource ", "");
					line = line.trim();
					res = line.split(" ")[0];
				}
				if (res != null) {
					res = fn + "." + res;
					if (pref == null) {
						pref = "public static final String " + fn
								+ "_prefix = " + res + ".getNameSpace();";
					}
					varList += " " + res + ".getLocalName(),";
				}
			}
			if (pref != null) {
				arrays.add(fn + "_list");
				prefLIst.add(fn + "_prefix");
				varList = varList.substring(0, varList.length() - 1);
				varList += "};";
				tf.add(pref);
				tf.add(varList);
			}
			in.closeFile();
		}
		String all_pref = "String[] pref_all = {";
		for (int i = 0; i < prefLIst.size(); i++)
			all_pref += " " + prefLIst.get(i) + ", ";
		all_pref = all_pref.substring(0, all_pref.length() - 1);
		all_pref += "};";

		String all_list = "String[][] list_all = {";
		for (int i = 0; i < arrays.size(); i++)
			all_list += arrays.get(i) + ", ";
		all_list = all_list.substring(0, all_list.length() - 1);
		all_list += "};";
		tf.add("public static final " + all_pref);
		tf.add("public static final " + all_list);
		tf.add("}");
		tf.writeFile("src/twi_project/core/Vocabulary.java", true);
		tf.closeFile();
	}

	private static ArrayList<String> words = null;

	private static void addIter(Iterator iter) {
		while (iter.hasNext()) {
			String val = iter.next().toString();
			String[] split = val.split("#");
			words.add(split[split.length - 1]);
		}
	}

	private static void setupWordList(OWLModel model) {
		words = new ArrayList<String>();
		ExtendedIterator iter = model.listAllOntProperties();
		addIter(iter);
		addIter(model.listClasses());
		addIter(model.listIndividuals());
		addIter(model.listIntersectionClasses());

	}

	public static ArrayList<String> getCompleteVocabulary(OWLModel model) {
		if (words == null)
			setupWordList(model);
		ArrayList<String> list = (ArrayList<String>) words.clone();
		for (int i = 0; i < Vocabulary.list_all.length; i++) {
			String pref = model.getNsURIPrefix(Vocabulary.pref_all[i]);
			if (pref == null)
				continue;
			for (int j = 0; j < Vocabulary.list_all[i].length; j++) {
				list.add(pref + ":" + Vocabulary.list_all[i][j]);
			}
		}
		return list;
	}

	public static ArrayList<String> getVocabulary(OWLModel model, String domain) {
		ArrayList<String> out = new ArrayList<String>();
		ExtendedIterator iter = model.listAllOntProperties();
		while (iter.hasNext()) {
			OntProperty prop = (OntProperty) iter.next();
			try {
				OntClass cl = (OntClass) prop.getDomain();
				if (!cl.getLocalName().equals(domain))
					continue;
				out.add(cl.getLocalName());
				ExtendedIterator subIter = cl.listInstances();
				while (subIter.hasNext()) {
					out.add(((Individual) subIter.next()).getLocalName());
				}
				return out;
			} catch (Exception e) {
			}
		}
		iter = model.listClasses();
		OntClass tmp = model.getOntClass(domain);
		while (iter.hasNext()) {
			OntClass cl = (OntClass) iter.next();
			if (cl.isAnon())
				continue;
			if (!cl.getLocalName().equals(domain))
				continue;
			out.add(cl.getLocalName());
			ExtendedIterator subIter = cl.listInstances();
			while (subIter.hasNext()) {
				out.add(((Individual) subIter.next()).getLocalName());
			}
			return out;
		}
		for (int i = 0; i < Vocabulary.pref_all.length; i++)
			if (Vocabulary.pref_all[i].equals(domain))
				return Utils.toArray(Vocabulary.list_all[i]);

		return out;
	}

	public static ArrayList<String> searchKey(String text, OWLModel model) {
		if (words == null)
			setupWordList(model);
		text = text.toLowerCase();
		ArrayList<String> out = new ArrayList<String>();

		for (int i = 0; i < words.size(); i++)
			if (words.get(i).toLowerCase().startsWith(text))
				out.add(words.get(i));

		for (int i = 0; i < Vocabulary.list_all.length; i++) {
			String pref = model.getNsURIPrefix(Vocabulary.pref_all[i]);
			if (pref == null)
				continue;
			for (int j = 0; j < Vocabulary.list_all[i].length; j++) {
				if (Vocabulary.list_all[i][j].toLowerCase().startsWith(text)) {
					out.add(pref + ":" + Vocabulary.list_all[i][j]);
				}
			}
		}
		return out;
	}

	public static ArrayList<String> getVocabulary(OWLModel model,
			ArrayList<String> domList) {
		ArrayList<String> out = new ArrayList<String>();
		for (int i = 0; i < domList.size(); i++)
			out.addAll(getVocabulary(model, domList.get(i)));
		return out;
	}

}
